package cn.edu.sgu.www.mhxysy.service.waiguan.impl;

import cn.edu.sgu.www.mhxysy.pager.Pager;
import cn.edu.sgu.www.mhxysy.consts.CustomDirectories;
import cn.edu.sgu.www.mhxysy.consts.CustomMessages;
import cn.edu.sgu.www.mhxysy.dto.waiguan.RoleShizhuangImageDTO;
import cn.edu.sgu.www.mhxysy.entity.waiguan.RoleShizhuangImage;
import cn.edu.sgu.www.mhxysy.mapper.waiguan.RoleShizhuangImageMapper;
import cn.edu.sgu.www.mhxysy.pager.waiguan.RoleShizhuangImagePager;
import cn.edu.sgu.www.mhxysy.service.waiguan.RoleShizhuangImageService;
import cn.edu.sgu.www.mhxysy.util.ImageUpdater;
import cn.edu.sgu.www.mhxysy.util.ObjectUtils;
import cn.edu.sgu.www.mhxysy.util.UploadUtils;
import cn.edu.sgu.www.mhxysy.util.WrapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.function.Consumer;

/**
 * @author 沐雨橙风ιε
 * @version 1.0
 */
@Slf4j
@Service
public class RoleShizhuangImageServiceImpl implements RoleShizhuangImageService {

    private final UploadUtils uploadUtils;
    private final ImageUpdater imageUpdater;
    private final RoleShizhuangImageMapper roleShizhuangImageMapper;

    @Autowired
    public RoleShizhuangImageServiceImpl(
            UploadUtils uploadUtils,
            ImageUpdater imageUpdater,
            RoleShizhuangImageMapper roleShizhuangImageMapper) {
        this.uploadUtils = uploadUtils;
        this.imageUpdater = imageUpdater;
        this.roleShizhuangImageMapper = roleShizhuangImageMapper;
    }

    @Override
    public String upload(MultipartFile file) throws IOException {
        String directory = CustomDirectories.DIRECTORY_ROLE_SHIZHUANG;

        return uploadUtils.upload(file, directory);
    }

    @Override
    public void insert(RoleShizhuangImageDTO roleShizhuangImageDTO) {
        RoleShizhuangImage roleShizhuangImage = toEntity(roleShizhuangImageDTO);

        roleShizhuangImage.setId(null);

        roleShizhuangImageMapper.insert(roleShizhuangImage);
    }

    @Override
    public void deleteById(Integer id) {
        roleShizhuangImageMapper.deleteById(id);
    }

    @Override
    public void updateById(RoleShizhuangImageDTO roleShizhuangImageDTO) {
        ObjectUtils.requireNonNull(roleShizhuangImageDTO.getId(), CustomMessages.ID_NOT_NULL);

        RoleShizhuangImage roleShizhuangImage = toEntity(roleShizhuangImageDTO);

        roleShizhuangImageMapper.updateById(roleShizhuangImage);
    }

    @Override
    public void updateImageById(Integer id, String image) {
        imageUpdater.updateImageById(image, new Consumer<Object>() {
            @Override
            public void accept(Object o) {
                UpdateWrapper<RoleShizhuangImage> updateWrapper = WrapperUtils.getUpdateWrapper(id, image);

                roleShizhuangImageMapper.update(updateWrapper.getEntity(), updateWrapper);
            }
        });
    }

    @Override
    public RoleShizhuangImage selectById(Integer id) {
        return roleShizhuangImageMapper.selectById(id);
    }

    @Override
    public Page<RoleShizhuangImage> selectByPage(RoleShizhuangImagePager pager) {
        QueryWrapper<RoleShizhuangImage> queryWrapper = new QueryWrapper<>();
        Page<RoleShizhuangImage> page = Pager.ofPage(pager);

        queryWrapper.eq(
                pager.getRoleId() != null,
                "role_id", pager.getRoleId()
        );
        queryWrapper.eq(
                pager.getShizhuangId() != null,
                "shizhuang_id", pager.getShizhuangId()
        );

        return roleShizhuangImageMapper.selectPage(page, queryWrapper);
    }

    private RoleShizhuangImage toEntity(RoleShizhuangImageDTO roleShizhuangImageDTO) {
        RoleShizhuangImage roleShizhuangImage = new RoleShizhuangImage();

        roleShizhuangImage.setId(roleShizhuangImageDTO.getId());
        roleShizhuangImage.setRoleId(roleShizhuangImageDTO.getRoleId());
        roleShizhuangImage.setShizhuangId(roleShizhuangImageDTO.getShizhuangId());

        return roleShizhuangImage;
    }

}